package com.an.blog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.an.blog.admin.domain.BlogArticleTag;
import com.an.blog.admin.domain.BlogTag;
import com.an.blog.admin.domain.bo.BlogTagBo;
import com.an.blog.admin.domain.vo.BlogTagVo;
import com.an.blog.mapper.BlogArticleTagMapper;
import com.an.blog.mapper.BlogTagMapper;
import com.an.blog.service.IBlogTagService;
import com.an.blog.web.domain.vo.BlogTagWebVo;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.page.TableDataInfo;
import com.an.common.exception.blog.BlogException;
import com.an.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 博客标签Service业务层处理
 *
 * @author Ann
 * @date 2023-05-08
 */
@RequiredArgsConstructor
@Service
public class BlogTagServiceImpl implements IBlogTagService {

    private final BlogTagMapper baseMapper;
    private final BlogArticleTagMapper articleTagMapper;

    /**
     * 查询博客标签
     *
     * @param tagId 标签id
     * @return {@link BlogTagVo }
     * @author: Ann
     * @createDate: 2023/05/18 18:55:02
     * @version: 1.0.0
     */
    @Override
    public BlogTagVo queryById(Long tagId) {
        return baseMapper.selectVoById(tagId);
    }

    /**
     * 查询博客标签列表
     *
     * @param bo 业务对象
     * @return {@link List }<{@link BlogTagVo }>
     * @author: Ann
     * @createDate: 2023/05/18 18:54:37
     * @version: 1.0.0
     */
    @Override
    public TableDataInfo<BlogTagVo> queryPageList(BlogTagBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogTag> lqw = buildQueryWrapper(bo);
        Page<BlogTagVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询博客标签列表
     *
     * @param bo 业务对象
     * @return {@link List }<{@link BlogTagVo }>
     * @author: Ann
     * @createDate: 2023/05/18 18:58:15
     * @version: 1.0.0
     */
    @Override
    public List<BlogTagVo> queryList(BlogTagBo bo) {
        LambdaQueryWrapper<BlogTag> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 建立查询包装
     *
     * @param bo 业务对象
     * @return {@link LambdaQueryWrapper }<{@link BlogTag }>
     * @author: Ann
     * @createDate: 2023/05/18 18:58:14
     * @version: 1.0.0
     */
    private LambdaQueryWrapper<BlogTag> buildQueryWrapper(BlogTagBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogTag> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTagName()), BlogTag::getTagName, bo.getTagName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BlogTag::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增博客标签
     *
     * @param bo 业务对象
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/18 18:58:12
     * @version: 1.0.0
     */
    @Override
    public Boolean insertByBo(BlogTagBo bo) {
        BlogTag add = BeanUtil.toBean(bo, BlogTag.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTagId(add.getTagId());
        }
        return flag;
    }

    /**
     * 修改博客标签
     *
     * @param bo 业务对象
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/18 18:58:10
     * @version: 1.0.0
     */
    @Override
    public Boolean updateByBo(BlogTagBo bo) {
        BlogTag update = BeanUtil.toBean(bo, BlogTag.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体
     * @author: Ann
     * @createDate: 2023/05/18 18:58:07
     * @version: 1.0.0
     */
    private void validEntityBeforeSave(BlogTag entity) {
        if (StringUtils.isNotEmpty(entity.getTagName()) && !checkTagNameUnique(entity)) {
            throw new BlogException("blog.tag.add.not.unique");
        }
    }

    /**
     * 检查标记名称独特
     *
     * @param entity 实体
     * @return boolean
     * @author: Ann
     * @createDate: 2023/05/18 18:58:05
     * @version: 1.0.0
     */
    private boolean checkTagNameUnique(BlogTag entity) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<BlogTag>()
                .eq(BlogTag::getTagName, entity.getTagName())
                .ne(ObjectUtil.isNotNull(entity.getTagId()), BlogTag::getTagName, entity.getTagId()));
        return !exist;
    }


    /**
     * 批量删除博客标签
     *
     * @param ids     id
     * @param isValid 是有效
     * @return {@link Boolean }
     * @author: Ann
     * @createDate: 2023/05/18 18:58:03
     * @version: 1.0.0
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            boolean exists = articleTagMapper.exists(new LambdaQueryWrapper<BlogArticleTag>()
                    .in(CollUtil.isNotEmpty(ids), BlogArticleTag::getTagId, ids));
            if (exists) {
                throw new BlogException("blog.tag.delete.error");
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 更新标签状态
     *
     * @param bo 业务对象
     * @return int
     * @author: Ann
     * @createDate: 2023/05/18 18:58:01
     * @version: 1.0.0
     */
    @Override
    public int updateTagStatus(BlogTagBo bo) {
        BlogTag blogTag = BeanUtil.toBean(bo, BlogTag.class);
        return baseMapper.updateById(blogTag);
    }

    /**
     * 查询id和名称列表
     *
     * @return {@link List }<{@link BlogTagWebVo }>
     * @author: Ann
     * @createDate: 2023/05/18 18:57:59
     * @version: 1.0.0
     */
    @Override
    public List<BlogTagWebVo> queryIdAndNameList() {
        List<BlogTagVo> blogTagVos = baseMapper.selectVoList(new LambdaQueryWrapper<BlogTag>()
                .select(BlogTag::getTagId, BlogTag::getTagName));
        return BeanUtil.copyToList(blogTagVos, BlogTagWebVo.class);
    }

    @Override
    public List<BlogTag> queryTagNamesByArticleId(Long articleId) {
        return baseMapper.queryTagNamesByArticleId(articleId);

    }
}
